#include "analyzer.h"
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>

bool isValidStrategy(const std::string& strategy) {
    const std::vector<std::string> validStrategies = {
        "technical", "fundamental", "combined"
    };
    return std::find(validStrategies.begin(), validStrategies.end(), strategy) != validStrategies.end();
}

void printUsage(const char* programName) {
    std::cerr << "用法: " << programName << " <数据文件> <策略>\n"
              << "可用策略:\n"
              << "  technical    - 技术分析\n"
              << "  fundamental  - 基本面分析\n"
              << "  combined     - 综合分析(默认)\n";
}

int main(int argc, char* argv[]) {
    if (argc < 3) {
        printUsage(argv[0]);
        return 1;
    }

    std::string dataFile = argv[1];
    std::string strategy = argv[2];

    if (!isValidStrategy(strategy)) {
        std::cerr << "错误: 无效的分析策略 '" << strategy << "'\n";
        printUsage(argv[0]);
        return 1;
    }

    try {
        std::cout << "=== 股票分析开始 ===\n";
        std::cout << "数据文件: " << dataFile << "\n";
        std::cout << "分析策略: " << strategy << "\n\n";

        // 加载数据
        std::cout << "1. 加载股票数据...\n";
        auto stocks = StockAnalyzer::loadFromCSV(dataFile);
        if (stocks.empty()) {
            std::cerr << "错误: 未加载到有效股票数据\n";
            std::cerr << "请检查文件路径和格式是否正确\n";
            return 1;
        }
        std::cout << "  成功加载 " << stocks.size() << " 条股票数据\n\n";

        // 分析并推荐
        std::cout << "2. 执行分析...\n";
        auto recommendations = StockAnalyzer::analyzeAndRecommend(stocks, strategy);
        
        // 保存结果
        std::cout << "3. 保存推荐结果...\n";
        StockAnalyzer::saveRecommendations(recommendations, "recommendations.csv");
        
        std::cout << "\n=== 分析完成 ===\n";
        std::cout << "输入数据: " << stocks.size() << " 条\n";
        std::cout << "推荐结果: " << recommendations.size() << " 条\n";
        std::cout << "结果文件: recommendations.csv\n";
        return 0;

    } catch (const std::exception& e) {
        std::cerr << "\n=== 分析失败 ===\n";
        std::cerr << "错误原因: " << e.what() << "\n";
        return 1;
    }
}